package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.List;
import java.util.logging.Logger;
import org.apache.commons.lang3.builder.CompareToBuilder;
//import org.apache.commons.lang3.builder.EqualsBuilder;
//import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.example.utils.SortedListOf;

public class GroupData implements Comparable<GroupData>{
	
	private String id;
	private String name;
	private String header;
	private String footer;
	
	public GroupData() {
	}

	public GroupData(String groupname, String header, String footer) {
		this.name = groupname;
		this.header = header;
		this.footer = footer;
	}

	@Override
	public String toString() {
		return "GroupData [id = " + id + ", name=" + name + ", header=" + header + ", footer="
				+ footer + "]";
	}
	
//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + ((name == null) ? 0 : name.hashCode());
//		return result;
//	}
	
//	@Override
//	public int hashCode() {
//		return HashCodeBuilder.reflectionHashCode(this, false);
//	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((footer == null) ? 0 : footer.hashCode());
		result = prime * result + ((header == null) ? 0 : header.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
//	@Override
//	public boolean equals(Object obj) {
//		if (this == obj)
//			return true;
//		if (obj == null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		GroupData other = (GroupData) obj;
//		if (name == null) {
//			if (other.name != null)
//				return false;
//		} else if (!name.equals(other.name))
//			return false;
//		return true;
//	}
	
//	@Override
//	public boolean equals(Object obj) {
//		return EqualsBuilder.reflectionEquals(this, obj, false);
//	}

	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GroupData other = (GroupData) obj;
		if (footer == null) {
			if (other.footer != null)
				return false;
		} else if (!footer.equals(other.footer))
			return false;
		if (header == null) {
			if (other.header != null)
				return false;
		} else if (!header.equals(other.header))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public int compareTo(GroupData other) {
		//return this.name.toLowerCase().compareTo(other.name.toLowerCase());
		return new CompareToBuilder()
		.append(name, other.name)
		.append(header, other.header)
		.append(footer, other.footer)	
		.build();
	}

	
	public GroupData withId(String id) {
		this.id = id;
		return this;
	}
	
	public GroupData withName(String name) {
		this.name = name;
		return this;
	}

	public GroupData withHeader(String header) {
		this.header = header;
		return this;
	}

	public GroupData withFooter(String footer) {
		this.footer = footer;
		return this;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getHeader() {
		return header;
	}

	public String getFooter() {
		return footer;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public void setFooter(String footer) {
		this.footer = footer;
	}	
	
	public static void assertDbGroupsEqual(
			SortedListOf<GroupData> expected, 
			SortedListOf<GroupData> actual) 
	{
		Logger.getGlobal().info("expected " + expected);
        Logger.getGlobal().info("actual   " + actual);
		assertThat("Groups lists have different sizes", expected.size(), equalTo(actual.size()));
		for (int i = 0; i < expected.size(); i++) 
		{
			GroupData exp = expected.get(i);
			GroupData act = actual.get(i);
			assertThat("Different Names", exp.getName(), equalTo(act.getName()));
			assertThat("Different Headers", exp.getHeader(), equalTo(act.getHeader()));
			assertThat("Different Footers", exp.getFooter(), equalTo(act.getFooter()));
		}
	}
	
	public static GroupData buildFromGroup(GroupData group){
		GroupData result = new GroupData();
		result.name = group.getName();		
		return result;
	}
	
	public static SortedListOf<GroupData> buildFromGroupList(List<GroupData> groupList) { 
		SortedListOf<GroupData> result = new SortedListOf<GroupData>();             
		for (GroupData c : groupList) {             
			result.add(GroupData.buildFromGroup(c));     
			}         
		return result;        
		}
	
}